package com.example.donghuihui.androidframe.ui;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.WindowManager;

import com.example.donghuihui.androidframe.AppManager;
import com.example.donghuihui.androidframe.BaseActivity;
import com.example.donghuihui.androidframe.R;
import com.example.donghuihui.androidframe.ui.activity.MainActivity;

import kr.co.namee.permissiongen.PermissionFail;
import kr.co.namee.permissiongen.PermissionGen;
import kr.co.namee.permissiongen.PermissionSuccess;

/**
 *欢迎页面  在做动态授权的处理
 */
public class WelcomeActivity extends BaseActivity {
    private static final long SPLASH_DISPLAY_LENGHT = 3000;
    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_welcome);
        requestStartPermissions();

    }

    /**
     * 申请用到的部分权限，其他权限在使用时单个获取
     */
    private void requestStartPermissions() {
        PermissionGen.with(this)
                .addRequestCode(100)
                .permissions(
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.CAMERA,
                        Manifest.permission.READ_PHONE_STATE
                )
                .request();
    }

    /**
     * 启动意图
     */
    private void startintentactivity() {
        startanim();
    }

    /**
     * 当用户拒绝权限时，提示设置权限或者退出程序
     */
    private void showPermissionDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("请提供应用的相关权限，否则可能导致应用无法正常运行");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        builder.setNegativeButton("去设置", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                requestStartPermissions();
            }
        });
        builder.create().show();
    }

    /*
     *授权后调用
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    /**
     * 所有权限都授权后调用方法
     */
    @PermissionSuccess(requestCode = 100)
    public void begin() {
        startintentactivity();
    }

    /**
     * 权限没有被全部授权时调用
     */
    @PermissionFail(requestCode = 100)
    public void doFailSomething() {
        showPermissionDialog();
    }
    @Override
    public void onBackPressed() {
        AppManager.getAppManager().finishActivity(this);
    }
    private void startanim() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
//                Boolean islogin = SPUtil.getSharedBooleanData(Constant.ISLOGIN,false);
//                if (islogin) {
//                    intent = new Intent(WelcomeActivity.this, MainActivity.class);
//                }else {
//                    intent = new Intent(WelcomeActivity.this,LoginActivity.class);
//                }
                intent=new Intent(WelcomeActivity.this,MainActivity.class);
                startActivity(intent);//启动意图
                finish(); //结束此界面
            }
        }, SPLASH_DISPLAY_LENGHT);
    }
}
